package biz;

import dao.CartDao;

import java.util.List;
import java.util.Map;

public class CartBiz {
    private CartDao dao = new CartDao();

    public List<Map<String, Object>> query(String uid){
        return dao.query(uid);
    }

    public void create(String product, String uid, String amount) throws BizException {

        if (dao.exists(product,uid) == 1){
            if (dao.modify(product, amount) <= 0){
                throw new BizException("商品数量不足!请及时补货!");
            }
            dao.modifyShop(uid,product,amount);
        }else {
            if (dao.modify(product, amount) <= 0) {
                throw new BizException("商品数量不足!请及时补货!");
            }
            dao.create(product, uid, amount);
        }
    }
    public void modify(String uid, String product, String amount) throws BizException {
        if (dao.modify(product, amount) <= 0){
            throw new BizException("商品数量不足!请及时补货!");
        }
        dao.modifyCart(uid, product, amount);
    }

    // 查看地址表中是否存在这个地址 不存在则生成
    public void queryAddress(String uid, String name, String telphone, String address) throws BizException {
        if (dao.queryAddress(uid, name, telphone, address) == 0){
            // 等于0 则不存在
            if (name == null ||name.trim().isEmpty()){
                throw new BizException("收货人不能为空!");
            }
            if (telphone == null || telphone.trim().isEmpty()){
                throw  new BizException("电话号码不能为空!");
            }
            if (address == null || address.trim().isEmpty()){
                throw new BizException("收货地址不能为空!");
            }
            if (telphone.length() != 11){
                throw new BizException("电话号码长度规定为11位");
            }
            //if (address.matches("(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)?(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)?(?<county>[^(区|市|县|旗|岛)]+区|.*?市|.*?县|.*?旗|.*?岛)?(?<village>.*)")){
            //    throw new BizException("请按省市区镇填写地址!");
            //}
            dao.createAddress(uid, name, telphone, address);
        }
    }

    public void createOrder(String uid, String amount, String address, String method, String telphone){
        dao.createOrder(uid, amount, address, method,telphone);
    }
    public void createItem(String uid, String product, String amount){
        dao.createItem(uid, product, amount);
    }
    public void removeCart(String uid, String product){
        dao.removeCart(uid, product);
    }

    public String queryProduct(String uid, String product, String amount) {
        return dao.queryProduct(uid, product, amount);
    }

    public void remove(String uid, String product, String amount){
        dao.remove(uid, product, amount);
    }
}
